﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace BEGDC.MIS.Office.Excel
{
    public class XmlDefinition
    {
        public XmlDefinition(string serviceName)
        {
            //Reader Definition from file
            string filename = Configuration.TemplatePath + "\\" + serviceName + "\\Definition.xml";
            tables = new XmlTableCollection();
            this.LoadFromFile(filename);
        }

        private void LoadFromFile(string fileName)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(fileName);

                XmlNode root = document.LastChild;   //Template
                XmlNode nodetable;
                XmlNode noderow;
                XmlNode nodeelement;

                XmlTable xmltable;
                XmlRow xmlrow;
                Element element;

                if (root.Name != "Template")
                    return;
                for (int i = 0; i < root.ChildNodes.Count; i++)
                {
                    nodetable = root.ChildNodes[i];   //DataTable
                    if (nodetable.Name != "DataTable")
                        continue;
                    xmltable = new XmlTable();
                    if (nodetable.Attributes["name"] != null)
                        xmltable.Name = nodetable.Attributes["name"].Value.Trim();
                    if (nodetable.Attributes["sheet"] != null)
                        xmltable.Sheet = nodetable.Attributes["sheet"].Value.Trim();

                    for (int j = 0; j < nodetable.LastChild.ChildNodes.Count; j++)
                    {
                        xmlrow = new XmlRow();
                        noderow = nodetable.LastChild.ChildNodes[j];   //DataRow
                        if (noderow.Name != "DataRow")
                            continue;

                        for (int k = 0; k < noderow.ChildNodes.Count; k++)
                        {
                            nodeelement = noderow.ChildNodes[k];   //Element
                            if (nodeelement.Name != "Element")
                                continue;
                            element = new Element();
                            if (nodeelement.Attributes["name"] != null)
                                element.Name = nodeelement.Attributes["name"].Value.Trim();
                            if (nodeelement.Attributes["position"] != null)
                                element.Position = nodeelement.Attributes["position"].Value.Trim();
                            if (nodeelement.Attributes["type"] != null)
                                element.Type = nodeelement.Attributes["type"].Value.Trim();
                            element.IsKey = false;
                            if ((nodeelement.Attributes["key"] != null) && (nodeelement.Attributes["key"].Value.Trim().ToLower() == "true"))
                            {
                                element.IsKey = true;
                            }
                            element.IsFixed = false;
                            if ((nodeelement.Attributes["fixed"] != null) && (nodeelement.Attributes["fixed"].Value.Trim().ToLower() == "true"))
                            {
                                element.IsFixed = true;
                            }
                            element.IsConvert = false;
                            if ((nodeelement.Attributes["convert"] != null) && (nodeelement.Attributes["convert"].Value.Trim().ToLower() == "true"))
                            {
                                element.IsConvert = true;
                            }
                            element.FixedValue = string.Empty;
                            if (nodeelement.Attributes["fixedvalue"] != null)
                            {
                                element.FixedValue = nodeelement.Attributes["fixedvalue"].Value;
                            }

                            if (nodeelement.Attributes["validation"] != null)
                            {
                                element.Validation = nodeelement.Attributes["validation"].Value;
                            }

                            if (nodeelement.Attributes["rule"] != null)
                            {
                                element.Rule = nodeelement.Attributes["rule"].Value;
                            }

                            if (nodeelement.Attributes["sample"] != null)
                            {
                                element.Sample = nodeelement.Attributes["sample"].Value;
                            }

                            xmlrow.Elements.Add(element);
                        }
                        xmltable.Rows.Add(xmlrow);
                    }
                    tables.Add(xmltable);
                }
            }
            catch(Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("01.0001", "Excel definition file format isn't  correct.", ex);
            }
        }

        private XmlTableCollection tables;
        public XmlTableCollection Tables
        {
            get
            {
                return tables;
            }
            set
            {
                tables = value;
            }
        }

        public static XmlDefinition CreateInstance(string serviceName)
        {
            //可以考虑使用Cache提高性能，目前由于性能压力不大，暂时不使用
            return new XmlDefinition(serviceName);
        }
    }    
}
